package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.atguigu.gulimall.product.vo.WithAttrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private CategoryDao categoryDao;
    @Resource
    private AttrAttrgroupRelationDao relationDao;
    @Resource
    private AttrDao attrDao;
    @Resource
    private AttrGroupDao attrGroupDao;
    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long id) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        Object key = params.get("key");
        if (null != key && StringUtils.isNotBlank(key.toString())) {
            wrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
        }
        if (id == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
        //select * from pms_attr_group where category_id = id and (attr_group_id = key or attr_group_name like %key%);
        wrapper.eq("catelog_id", id);
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public void relationDelete(List<AttrGroupRelationVo> attrGroupRelationVo) {
        List<AttrAttrgroupRelationEntity> list = attrGroupRelationVo.stream().map((item) -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, entity);
            return entity;
        }).collect(Collectors.toList());
        relationDao.deleteBatchRelation(list);
    }

    @Override
    public List<WithAttrVo> withAttr(Long catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> attrGroupWrapper = new LambdaQueryWrapper<>();
        attrGroupWrapper.eq(AttrGroupEntity::getCatelogId, catelogId);
        List<AttrGroupEntity> groupEntityList = baseMapper.selectList(attrGroupWrapper);
        List<WithAttrVo> attrVos = groupEntityList.stream().map((item) -> {
            WithAttrVo vo = new WithAttrVo();
            BeanUtils.copyProperties(item, vo);

//            List<AttrEntity> attrs = attrService.getAttrRelation(item.getAttrGroupId());
//            List<AttrVo> vos = attrs.stream().map((attr) -> {
//                AttrVo attrVo = new AttrVo();
//                BeanUtils.copyProperties(attr, attrVo);
//                attrVo.setAttrGroupId(item.getAttrGroupId());
//                return attrVo;
//            }).collect(Collectors.toList());
//            vo.setAttrs(vos);
            return vo;
        }).collect(Collectors.toList());
        // 设置 WithAttrVo.attrs
        List<Long> collect = groupEntityList.stream().map((item) -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AttrAttrgroupRelationEntity::getAttrGroupId, collect);
        List<AttrAttrgroupRelationEntity> list = relationDao.selectList(lambdaQueryWrapper);
        List<Long> attrIds = list.stream().map((item) -> {
            return item.getAttrId();
        }).collect(Collectors.toList());
        if (attrIds != null && attrIds.size() > 0) {
            LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(AttrEntity::getAttrId, attrIds);
            List<AttrEntity> entities = attrDao.selectList(wrapper);
            for (WithAttrVo attrVo : attrVos) {
                Long groupId = attrVo.getAttrGroupId();
                ArrayList<AttrVo> vos = new ArrayList<>();
                for (AttrAttrgroupRelationEntity relationEntity : list) {
                    if (groupId.equals(relationEntity.getAttrGroupId())) {
                        Long attrId = relationEntity.getAttrId();
                        for (AttrEntity entity : entities) {
                            if (entity.getAttrId().equals(attrId)) {
                                AttrVo vo = new AttrVo();
                                BeanUtils.copyProperties(entity, vo);
                                vos.add(vo);
                            }
                        }
                    }
                }
                attrVo.setAttrs(vos);
            }
        }

        return attrVos;
    }

}